﻿

using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using iemtm.WeChat.Core.Extension;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;

namespace iemtm.WeChat.Core.Managers
{
    /// <summary>
    /// 获取一些基站信息 区域 工单 等通用信息
    /// </summary>
    public class InfoManager : IInfoManager, IScoped
    {
        private readonly IRepository<Company> _companyRepository;
        private readonly IRepository<Area> _areaRepository;
        private readonly IRepository<AnnounceList> _announceListRepository; 
        private readonly IRepository<CommType> _commTypeRepository;
        private readonly IRepository<AnnounceGen> _announceGenRepository;
        private readonly IRepository<AnnounceStateXLog> _announceStateXLogRepository;
        private readonly IRepository<BaseStation> _baseStationRepository;
        private readonly IRepository<AnnFee> _annFeeRepository;
        private readonly IRepository<ChangeAnnGenset> _changeAnnGensetRepository;
        private readonly IRepository<ReBinAnnGenset> _reBinAnnGensetRepository;
        private readonly IRepository<AnnounceAudit> _announceAuditRepository;
        private readonly IRepository<GenRecord> _genRecordRepository;
        private readonly IRepository<WXEditor> _wXEditorRepository;
        private readonly IRepository<UserAccount> _userAccountRepository;
        


        public InfoManager(IRepository<Company> companyRepository
            , IRepository<Area> areaRepository
            , IRepository<AnnounceList> announceListRepository
            , IRepository<CommType> commTypeRepository
            , IRepository<AnnounceGen> announceGenRepository
            , IRepository<AnnounceStateXLog> announceStateXLogRepository
            , IRepository<BaseStation> baseStationRepository 
            , IRepository<AnnFee> annFeeRepository
            , IRepository<ChangeAnnGenset> changeAnnGensetRepository
            , IRepository<ReBinAnnGenset> reBinAnnGensetRepository
            , IRepository<AnnounceAudit> announceAuditRepository
            , IRepository<GenRecord> genRecordRepository
            , IRepository<WXEditor> wXEditorRepository
            , IRepository<UserAccount> userAccountRepository
            )
        {
            _companyRepository = companyRepository;
            _areaRepository = areaRepository;
            _announceListRepository = announceListRepository;
            _commTypeRepository = commTypeRepository;
            _announceGenRepository = announceGenRepository;
            _announceStateXLogRepository = announceStateXLogRepository;
            _baseStationRepository = baseStationRepository;
            _annFeeRepository = annFeeRepository;
            _changeAnnGensetRepository = changeAnnGensetRepository;
            _reBinAnnGensetRepository = reBinAnnGensetRepository;
            _announceAuditRepository = announceAuditRepository;
            _genRecordRepository = genRecordRepository;
            _wXEditorRepository = wXEditorRepository;
            _userAccountRepository = userAccountRepository;
        }

        #region 公司
        /// <summary>
        /// 获取公司信息
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public async  Task<Company> GetCompanyByCodeAsync(string Code)
        {
          return  await _companyRepository.FirstOrDefaultAsync(u => u.CompanyCode==Code);
        }

        /// <summary>
        /// 获取公司列表 tree
        /// </summary>
        /// <returns></returns>
        public dynamic GetCompanys(UserAccount LoginInfo)
        {



            if (LoginInfo != null)
            {
                //判断用户的数据权限（仅通过区域过滤数据）
                var authority = ($"SELECT * FROM dbo.Permission p WHERE p.MenuCode='2001' AND p.RoleCode=(SELECT RoleCode FROM dbo.UserAccount WHERE UserCode='{LoginInfo.UserCode}')").SqlQuery();
                var count = authority.Rows.Count;
                string sql = "";
                if (count == 0)
                {
                    sql = "and companycode like '" + LoginInfo.CompanyCode + "%'";
                }
                var AllGetCompanys = ($" select * from company where areacode like '{LoginInfo.AreaCode}%'  {sql} ").SqlQuery();
                return AllGetCompanys.ToJson();
            }
            return "";
        }

        #endregion


        /// <summary>
        /// 获取区域信息
        /// </summary>
        /// <param name="Code"></param>
        /// <returns></returns>
        public async Task<Area> GetAreaByCodeAsync(string Code)
        {
            return await _areaRepository.FirstOrDefaultAsync(u => u.AreaCode == Code);
        }
       

        /// <summary>
        /// 获取基站信息
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<BaseStation> GetBaseStationFirstAsync(Expression<Func<BaseStation, bool>> predicate)
        {
            return await _baseStationRepository.FirstOrDefaultAsync(predicate);
        }
        /// <summary>
        /// 获取基站信息-分页
        /// </summary>
        /// <param name="predicate"></param>
        /// <param name="pageIndex">页面</param>
        /// <param name="pageSize">页数</param>
        /// <returns></returns>
        public async Task<List<BaseStation>> GetBaseStationListAsync(Expression<Func<BaseStation, bool>> predicate,int pageIndex,int pageSize)
        {
            return await _baseStationRepository.Where(predicate).OrderBy(u=> u.BaseStationID).Skip((pageIndex-1)* pageSize).Take(pageSize).ToListAsync();
        }

        /// <summary>
        /// 获取工单计费信息
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<AnnFee> GetAnnFeeFirstAsync(Expression<Func<AnnFee, bool>> predicate)
        {
            return await _annFeeRepository.FirstOrDefaultAsync(predicate);
        }
        /// <summary>
        /// 获取工单计费信息
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public async Task<bool> AddAnnFeeFirstAsync(AnnFee entity)
        {
            var  T= (await _annFeeRepository.InsertAsync(entity)).Entity;
            if (T != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        #region 工单信息
        /// <summary>
        /// 获取工单信息
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <returns></returns>
        public async Task<AnnounceList> GetAnnFirstOrDefaultAsync(Expression<Func<AnnounceList, bool>> predicate)
        {
            return await _announceListRepository.FirstOrDefaultAsync(predicate);
        }
        /// <summary>
        /// 更新工单信息
        /// </summary>
        /// <param name="ann">更新的实体</param>
        /// <param name="UpdateColumn">更新的列</param>
        /// <returns></returns>
        public  bool UpdateAnnUpdateInclude(AnnounceList ann,string[] UpdateColumn)
        {
           var T=  _announceListRepository.UpdateIncludeNow(ann, UpdateColumn);
            if (T != null)
            {
                return true;
            }
            else
            {
                return false;
            }

        }
        #endregion

        /// <summary>
        /// 获取状态类型
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <returns></returns>
        public async Task<CommType> GetCommTypeFirstOrDefaultAsync(Expression<Func<CommType, bool>> predicate)
        {
            return await _commTypeRepository.FirstOrDefaultAsync(predicate);
        }

        /// <summary>
        /// 获取工单改中间表
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <returns></returns>
        public async Task<List<ChangeAnnGenset>> GetChangeAnnGensetListAsync(Expression<Func<ChangeAnnGenset, bool>> predicate)
        {
            return await _changeAnnGensetRepository.Where(predicate).ToListAsync();
        }

        /// <summary>
        /// 添加工单-绑定油机改中间表
        /// </summary>
        /// <param name="entity">条件</param>
        /// <returns></returns>
        public async Task<bool> AddChangeAnnGensetAsync(ChangeAnnGenset entity)
        {
            var T= (await _changeAnnGensetRepository.InsertAsync(entity)).Entity;
            if (T != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取工单的所有发电记录id
        /// </summary>
        /// <param name="Anncode"></param>
        /// <returns></returns>
        public async Task<List<int>> GetAnnGenRecordIdAsync(string Anncode)
        {
           return await _announceGenRepository.Where(u => u.AnnounceCode == Anncode).Select(u => u.GenRecordId).ToListAsync();
        }
        /// <summary>
        /// 获取工单发电记录的列表
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<List<AnnounceGen>> GetAnnounceGenListAsync(Expression<Func<AnnounceGen, bool>> predicate)
        {
            return await _announceGenRepository.Where(predicate).ToListAsync();
        }
        


        /// <summary>
        /// 添加工单操作记录
        /// </summary>
        /// <param name="entity"></param>
        public async Task<AnnounceStateXLog> AddAnnounceStateXLog(AnnounceStateXLog entity)
        {
            
             return (await _announceStateXLogRepository.InsertAsync(entity)).Entity;
        }
        /// <summary>
        /// 工单绑定
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<List<ReBinAnnGenset>> GetReBinAnnGensetListAsync(Expression<Func<ReBinAnnGenset, bool>> predicate)
        {
            return await _reBinAnnGensetRepository.Where(predicate).ToListAsync();
        }
        /// <summary>
        /// 添加-工单绑定油机改中间表
        /// </summary>
        /// <param name="entity">条件</param>
        /// <returns></returns>
        public async Task<bool> AddReBinAnnGensetAsync(ReBinAnnGenset entity)
        {
            var T = (await _reBinAnnGensetRepository.InsertAsync(entity)).Entity;
            if (T != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 添加-工单审核表
        /// </summary>
        /// <param name="entity">条件</param>
        /// <returns></returns>
        public async Task<bool> AddAnnounceAuditAsync(AnnounceAudit entity)
        {
            var T = (await _announceAuditRepository.InsertAsync(entity)).Entity;
            if (T != null)
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// 获取审核工单信息
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<AnnounceAudit> GetAnnounceAuditFirstAsync(Expression<Func<AnnounceAudit, bool>> predicate)
        {
            return await _announceAuditRepository.FirstOrDefaultAsync(predicate);
        }

        /// <summary>
        /// 更新工单审核信息
        /// </summary>
        /// <param name="ann">更新的实体</param>
        /// <param name="UpdateColumn">更新的列</param>
        /// <returns></returns>
        public bool UpdateAnnounceAuditInclude(AnnounceAudit ann, string[] UpdateColumn)
        {
            var T =  _announceAuditRepository.UpdateIncludeNow(ann, UpdateColumn).Entity;
            if (T != null)
            {
                return true;
            }
            else
            {
                return false;
            }

        }

        /// <summary>
        /// 获取审核工单信息
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<GenRecord> GetGenRecordFirstAsync(Expression<Func<GenRecord, bool>> predicate)
        {
            return await _genRecordRepository.FirstOrDefaultAsync(predicate);
        }

        /// <summary>
        /// 获取微信通知信息
        /// </summary>
        /// <param name="predicate"></param>
        /// <returns></returns>
        public async Task<WXEditor> GetWXEditorFirstAsync(Expression<Func<WXEditor, bool>> predicate)
        {
            return await _wXEditorRepository.FirstOrDefaultAsync(predicate);
        }


        /// <summary>
        /// 获取用户
        /// </summary>
        /// <param name="predicate">条件</param>
        /// <returns></returns>
        public async Task<List<UserAccount>> GetUserAccountListAsync(Expression<Func<UserAccount, bool>> predicate)
        {
            return await _userAccountRepository.Where(predicate).ToListAsync();
        }
    }
}
